package com.sfy.core.example.service.tcp;

import com.sfy.core.example.service.HttpServer;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer;

public class VertxTcpServer implements HttpServer {

    private byte[] handleRequest(byte[] requestData){
        return "Hello client".getBytes();
    }


    @Override
    public void doStart(int port) throws Exception {
        Vertx vertx = Vertx.vertx();

        NetServer server = vertx.createNetServer();

        server.connectHandler(socket -> {
            socket.handler(buffer -> {
                byte[] requestData = buffer.getBytes();
                byte[] responseData = handleRequest(requestData);
                // 发送响应
                socket.write(Buffer.buffer(responseData));
            });
        });

        server.listen(port,result->{
            if(result.succeeded()){
                System.out.println("Server started on port "+port);
            }else {
                System.out.println("Server failed to start on port "+port);
            }
        });
    }

    public static void main(String[] args) throws Exception {
        new VertxTcpServer().doStart(8888);
    }

}
